package Board;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

import common.ClampFactory;

public class BoardMyba {
	private SqlSession session;

	int listSize = 5; // 한 페이지에 보여질 행수
	int blockSize = 5; // 보여질 페이지 이동 번호 수.
	int totSize = 0;// 검색된 데이터 총 개수.
	int totPage = 0; // 전체 페이지수
	int totBlock = 0; // 전체블록수
	int nowPage = 1;// 현재 페이지
	int nowBlock = 1;// 현재 블럭
	int startNo = 1; // 목록의 시작위치
	int endNo = 3; // 목록의 끝 위치
	int startPage = 1; // 현재 블럭의 시작 페이지번호
	int endPage = 2; // 현재 블럭의 끝 페이지 번호

	int fileSize = 10 * 1024 * 1024;
	String uploadPath; // 파일저장
	String fileEncoding = "utf-8";

	BoardVo vo;

	public BoardMyba() {
		try {
			session = ClampFactory.getFactory().openSession();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void computePage() {
		totPage = (int) Math.ceil(totSize * 1.0 / listSize);
		totBlock = (int) Math.ceil(totPage * 1.0 / blockSize);// 넘어가는
																// 페이지.(블록페이지)
		
		nowBlock = (int) Math.ceil(nowPage * 1.0 / blockSize);
		endPage = nowBlock * blockSize;
		startPage = endPage - blockSize + 1;
		if (totPage < endPage)
			endPage = totPage;

		endNo = nowPage * listSize;
		startNo = endNo - listSize + 1;
		if (totSize < endNo)
			endNo = totSize;
		
		
	}

	public ArrayList<BoardVo> list() {
		
		nowPage = vo.getNowPage();
		ArrayList<BoardVo> list = null;
		try {
			int cnt = (int)session.selectOne("board.search", vo);
			
			totSize = cnt;
			computePage();
			
			vo.setStartNo(startNo);
			vo.setEndNo(endNo);
			
			list = (ArrayList<BoardVo>) session.selectList("board.list", vo);

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return list;
	}

	public ArrayList<BoardVo> mlist() {
		ArrayList<BoardVo> mlist = null;
		try {
			mlist = (ArrayList<BoardVo>) session.selectList("board.gonji_list",
					vo);
			session.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return mlist;
	}

	public BoardVo view() {
		BoardVo view = null;

		try {
			
			view = (BoardVo) session.selectOne("board.view", vo);
			clicks();
			session.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return view;
	}

	public void clicks() {
		try {
			session.update("board.view_clicks", vo);
			
			session.commit();
		} catch (Exception ex) {

		}
	}

	public boolean insert() {
		boolean flag = false;
		String realPath = vo.getRequest().getServletContext()
				.getRealPath("WebContent");
		
		String path = realPath.replace("\\WebContent", "");
		String pro = path.substring(path.lastIndexOf("\\") + 1, path.length());
		/*uploadPath = realPath.substring(0, realPath.indexOf(".")) + pro
				+ "\\WebContent\\homepage\\board\\boardData\\";*/
		
		try {
			File file = new File(uploadPath);
			if (!file.exists()) {
				file.mkdir();
			}
			MultipartRequest mr = new MultipartRequest(vo.getRequest(),
					uploadPath, fileSize, fileEncoding,
					new DefaultFileRenamePolicy());
			vo.setBo_title(mr.getParameter("bo_title"));
			vo.setBo_content(mr.getParameter("bo_content"));
			vo.setBo_part(mr.getParameter("bo_part"));
			vo.setBo_checked(mr.getParameter("bo_checked"));
			vo.setBo_st(mr.getParameter("bo_st"));
			vo.setBo_file(mr.getFilesystemName("bo_file"));
			
			

			
			if (vo.getBo_file() == null || vo.getBo_file().equals("")) {
				vo.setBo_file("");
			}
			/*if(vo.getBo_content() == "<br>" || vo.getBo_content().equals("<br>")){
				
			}*/
			if (vo.getBo_checked() == null || vo.getBo_checked().equals("")) {
				vo.setBo_checked("N");
			}
			
			

			int board = session.insert("board.insert", vo);
			if (board > 0) {

				flag = true;
				session.commit();
				session.close();
			} else {

				flag = false;
				session.rollback();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return flag;
	}

	public boolean modify() {
		boolean flag = false;
		String realPath = vo.getRequest().getServletContext()
				.getRealPath("WebContent");
		String path = realPath.replace("\\WebContent", "");
		String pro = path.substring(path.lastIndexOf("\\") + 1, path.length());
		/*uploadPath = realPath.substring(0, realPath.indexOf(".")) + pro
				+ "\\WebContent\\homepage\\board\\boardData\\";*/

		try {
			File file = new File(uploadPath);
			if (!file.exists()) {
				file.mkdir();
			}
			MultipartRequest mr = new MultipartRequest(vo.getRequest(),
					uploadPath, fileSize, fileEncoding,
					new DefaultFileRenamePolicy());

			vo.setBo_title(mr.getParameter("bo_title"));
			vo.setBo_checked(mr.getParameter("bo_checked"));
			vo.setBo_no(mr.getParameter("bo_no"));
			vo.setBo_content(mr.getParameter("bo_content"));
			vo.setBo_part(mr.getParameter("bo_part"));
			String oldfile = mr.getParameter("bo_file");
			vo.setNew_bo_file(mr.getFilesystemName("new_bo_file"));
			String fileDelete = mr.getParameter("fileDelete");
			String nowfile = vo.getNew_bo_file();
			
			if (mr.getParameter("bo_part") != null) {
				if (mr.getParameter("bo_part").equals("공지사항"))
					vo.setBo_part("공지사항");
				else if (mr.getParameter("bo_part").equals("인사팀"))
					vo.setBo_part("인사팀");
				else if (mr.getParameter("bo_part").equals("개발팀"))
					vo.setBo_part("개발팀");
				else if (mr.getParameter("bo_part").equals("총무팀"))
					vo.setBo_part("총무팀");
				else if (mr.getParameter("bo_part").equals("홍보팀"))
					vo.setBo_part("홍보팀");
				else if (mr.getParameter("bo_part").equals("건의사항"))
					vo.setBo_part("건의사항");
			}
			
			//중요공지에서 일반공지로 변경
			if (vo.getBo_checked() == null || vo.getBo_checked().equals("")) {
				vo.setBo_checked("N");
			}
			// 파일이 삭제되었을 때
			if (fileDelete.equals("fileDelete")) {
				file = new File(uploadPath + "/" + oldfile);
				if (file.exists())
					file.delete();
				if (vo.getBo_file() == null || vo.getBo_file().equals("")) {
					vo.setBo_file("");
				}
				session.update("board.fileDelete", vo);
				session.commit();

			}

			
			
			int board = session.update("board.modify", vo);
			
			if (board > 0) {
				
				flag = true;
				session.commit();
			} else {
				flag = false;
				session.rollback();
			}

			// 파일이 새로 추가되거나 파일을 삭제하고 다시 올릴 때.
			if (nowfile != null && nowfile != "") {
				board = session.update("board.fileInsert", vo);
				if (board > 0) {

					flag = true;
					session.commit();
				} else {
					flag = false;
					session.rollback();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return flag;
	}

	public boolean delete() {
		boolean flag = false;
	
		try {
			int board = session.update("board.delete", vo);
			if (board > 0) {
				flag = true;
				session.commit();
			} else {
				flag = false;
				session.rollback();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return flag;
	}

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public int getBlockSize() {
		return blockSize;
	}

	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}

	public int getTotSize() {
		return totSize;
	}

	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}

	public int getTotPage() {
		return totPage;
	}

	public void setTotPage(int totPage) {
		this.totPage = totPage;
	}

	public int getTotBlock() {
		return totBlock;
	}

	public void setTotBlock(int totBlock) {
		this.totBlock = totBlock;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getNowBlock() {
		return nowBlock;
	}

	public void setNowBlock(int nowBlock) {
		this.nowBlock = nowBlock;
	}

	public int getStartNo() {
		return startNo;
	}

	public void setStartNo(int startNo) {
		this.startNo = startNo;
	}

	public int getEndNo() {
		return endNo;
	}

	public void setEndNo(int endNo) {
		this.endNo = endNo;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public String getUploadPath() {
		return uploadPath;
	}

	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}

	public String getFileEncoding() {
		return fileEncoding;
	}

	public void setFileEncoding(String fileEncoding) {
		this.fileEncoding = fileEncoding;
	}

	public BoardVo getVo() {
		return vo;
	}

	public void setVo(BoardVo vo) {
		this.vo = vo;
	}
}
